<?php
// 内容队列
set_time_limit(0);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/Image.class.php');
include_once('../lib/Http.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/Cut.class.php');
include_once('../lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$redis->auth(REDIS_PASS);

$arg_befrom = $argc>1 ? $argv[1] : '';

$GLOBALS['config']['master']['db_database'] = 'suucn';
$GLOBALS['config']['slave']['db_database']  = 'suucn';

$model = new Db;
$snoopy = new Snoopy;
$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||gouyg_queue_news');

$limit = 100;
while ($limit--) {
    $stop = $redis->get('LNPAN_QUEUE_STOP');
    if($stop) {
        echo 'stop';
        break;
    }

    $where = '1 ';
    if(!empty($arg_befrom)) {
        $where .= "AND befrom='$arg_befrom' ";
    }
    $where .= "AND title is null ";

    $info = $model->find('soso', $where, "id asc");
    //var_dump($info);
    if(!$info) {
        if($limit < 98) {
            file_get_contents("http://www.gouyg.com/?m=admin&a=delcache&isajax=1&dc_login=0623");
            file_get_contents("http://www.gouyg.com/?m=admin&a=toindex&isajax=1&dc_login=0623");
            file_get_contents("http://www.gouyg.com/?m=admin&a=tonews&isajax=1&dc_login=0623");

            file_get_contents("http://www.lnsuu.com/?m=admin&a=delcache&isajax=1&dc_login=0623");
            file_get_contents("http://www.lnsuu.com/?m=admin&a=toindex&isajax=1&dc_login=0623");
            file_get_contents("http://www.lnsuu.com/?m=admin&a=tonews&isajax=1&dc_login=0623");
        }
        sleep(2);
        break;
    }

    $host_suffix = $info['befrom']=='i7wu' ? '.cn' : '.com';
    $url  = 'http://www.'.$info['befrom'].$host_suffix.$info['beurl'];
    $parse = parse_url($url);
    $host  = $parse['host'];
    $site  = $info['befrom']; 
    echo $url;
    $file  = getFile($url, $host);
    //var_dump($file);
    $html  = getNews($file, $site);
    //print_r($html); exit;
    if(!empty($html['title']) && !empty($html['betype']) && !empty($html['content']) && !empty($html['downpath'])){
        $html['sodate'] = date("Y-m-d H:i:s");
        $html['betype'] = resetClass($html['betype']);
        list($html['classid'], $html['typeid']) = getClassID($html['betype'], $model);

        $ret = $model->save("soso", "id='$info[id]'", $html);
        if($ret){
            $html['content'] = str_replace('wrshu', 'gouyg', $html['content']);
            $html['content'] = str_replace('qisuu', 'gouyg', $html['content']);
            $content_data = array('bookid'=>$info['id'],'content'=>$html['content']);
            $model->add("soso_content", $content_data);
            $content_id  = mysql_insert_id();
        }
        $json['msg'] = ' =>入库成功! id:'.$info['id'].' content_id:'.$content_id;
    }
    else{ //采集数据不完整
        $json['msg'] = ' =>采集数据不完整! id:'.$info['id'];
        $log = date('Y-m-d H:i').'||'.$url.'||'.json_encode($html);
        $redis->lPush('Gouyg_news_fail', $log);

        if(!empty($html['title']) && !empty($html['betype']) && !empty($html['content']) && empty($html['downpath'])){
            $model->query("delete from soso where id='$info[id]'");
            echo2('下载地址未获取到，需重新验证规则');
            //sleep(2);
        } else {
            $model->query("delete from soso where id='$info[id]'");
        }
    }
    print_r($html);
    print_r($json);
    //break;
}

$model->close();
$redis->hDel(MH_PIDS, $pid);

sleep(5);
function getNews($web, $site)
{
    if($site=='qisuu'){
        $split['title']  = array('<h1>','</h1>');
        $split['size']   = array('<li class="small">文件大小：','</li>');
        $split['betype'] = array('<div class="wrap position">','</span>');
        $split['author'] = array('<li class="small">书籍作者：','</li>');
        $split['content'] = array('<div class="showInfo">','<a name="download">');
        $split['downpath'] = array("<a class=\"downButton\" href='","'");
    }
    elseif($site=='i7wu'){
        $split['title']  = array('<h1>','<span id="author">');
        $split['size']   = array('<em>','</em>');
        $split['betype'] = array('class="lan">','</a>');
        $split['author'] = array('<span id="author">','</span>');
        $split['content'] = array('<h3>内容简介</h3>','</div>');
        $split['downpath'] = array('<li class="bd"><a href="','"');
    }
    elseif($site=='bookben'){
        $split['title']  = array('<h2>','</h2>');
        $split['size']   = array('<span>小说大小：</span>','</dd>');
        $split['betype'] = array('<span>小说类别：</span>','</dd>');
        $split['author'] = array('<span>作者：</span>','</dd>');
        $split['content'] = array('<dd class="xgjsdd">','</p>');
        $split['downpath'] = array('<ul class="xz2ul"><li><a href="','"');
    }
    elseif($site=='ibook8'){
        $split['title']  = array('<dd class="downInfoRowL">','</b>');
        $split['size']   = array('<span>小说大小：</span>','</dd>');
        $split['betype'] = array('<b>电子书分类：</b>','<br/>');
        $split['author'] = array('<b>作者：</b>','<br/>');
        $split['content'] = array('<div id="mainSoftIntro">','</div>');
        $split['downpath'] = array('<strong>下载至电脑</strong> <a href="','"');
    }
    $return = array();
    
    $web = iconv("GBK", "UTF-8//IGNORE", $web);
    $return['title']    = cut($split['title'][0],$split['title'][1],$web);
    $return['size']     = cut($split['size'][0],$split['size'][1],$web);
    $return['betype']   = cut($split['betype'][0],$split['betype'][1],$web);
    $return['author']   = cut($split['author'][0],$split['author'][1],$web);
    $return['content']  = cut($split['content'][0],$split['content'][1],$web);
    $return['downpath'] = cut($split['downpath'][0],$split['downpath'][1],$web);

    if($site=='qisuu' || $site=='wrshu'){
        preg_match('/<a href="\/\w+\/\w+\/\w+\/">(.+?)<\/a>/i', $return['betype'], $out);
        $return['betype'] = $out[1];
    }
    elseif($site=='bookben') {
        $arr = explode(' ', $return['betype']);
        $return['betype'] = $arr[0];
    }
    elseif($site=='ibook8') {
        $arr = explode(' ', $return['betype']);
        $return['betype'] = $arr[0];
        $return_downpath = $return['downpath'];
        global $snoopy, $redis;

        if (!$return_downpath) {
            $redis->set('error:b'.md5($return['title']), $web);
        }

        $snoopy->_redirectaddr = true;
        $snoopy->fetch($return_downpath);
        $web = iconv("GBK", "UTF-8//IGNORE", $snoopy->results);
        $web2 = cut('<td valign="top" width="290">', '<td width="380">', $web);
        preg_match_all('/href="(.+?)"/i', $web2, $matches);
        $return['downpath'] = $matches[1][1];

        if (!$return['downpath']) {
            $redis->set('error:a'.md5($return_downpath), "{$return_downpath} ".$snoopy->response_code." ".print_r($snoopy->headers,true)." {$web}");
        }
    }
    $return['title']   = str_replace('《','',$return['title']);
    $return['title']   = str_replace('》','',$return['title']);
    $return['title']   = str_replace('全集','',$return['title']);
    $return['title']   = str_replace('TXT','',$return['title']);
    $return['title']   = str_replace('txt','',$return['title']);
    $return['title']   = str_replace('下载','',$return['title']);
    $return['title']   = str_replace('&nbsp;','',$return['title']);
    $return['title']   = strip_tags($return['title']);
    $return['title']   = trim($return['title']);
    $return['betype']  = str_replace('下载', '', $return['betype']);
    $return['betype']  = trim($return['betype']);
    $return['author']  = strip_tags($return['author']);
    $return['author']  = str_replace('作者：', '', $return['author']);
    $return['author']  = str_replace('连载中', '', $return['author']);
    $return['author']  = str_replace('&nbsp;', '', $return['author']);
    $return['author']  = str_replace('-', '', $return['author']);
    $return['content'] = strip_tags($return['content'],'<br>'); 
    //print_r($return);exit;
    return $return;
}

//获取栏目ID
function getClassID($betype, $model)
{
    $type_info = $model->find('betype', "typename='$betype'");
    if($type_info) {
        return array($type_info['classid'], $type_info['id']);
    } else {
        return array(13, 134);
    }
}

//重设栏目
function resetClass($betype)
{
    $arr = array(
        '其它TXT'=>'其他书籍','军事历史'=>'历史军事','古代言情'=>'穿越架空','名著古典'=>'古典名著','官场沉浮'=>'官场都市','推理惊险'=>'推理侦探',
        '游戏同人'=>'动漫同人','现代言情'=>'都市言情','科幻奇幻'=>'科幻玄幻','科幻穿越'=>'科幻世界','中国史'=>'社会科学','人力资源'=>'经营管理',
        '穿越小说'=>'架空历史','网游竞技'=>'虚拟网游','耽美同人'=>'耽美纯爱','菁菁校园'=>'青春校园','健康保健'=>'生活百科','商战风云'=>'官场都市',
        '言情小说'=>'浪漫言情','都市现代'=>'都市生活','世界史'=>'社会科学','励志成功'=>'励志教育','科普读物'=>'自然科学','艺术长廊'=>'人文环境',
        '处世哲学'=>'哲理宗教','婚姻情感'=>'社会交际','心理研究'=>'言论调查','科普图书'=>'自然科学','财富创业'=>'励志教育','市场策划'=>'经营管理',
        '灵异推理'=>'灵异神怪','人生百态'=>'品味人生','历史地理'=>'人文环境','宗教哲学'=>'哲理宗教','当代文学'=>'现代文学','市场营销'=>'经营管理',
        '经济金融'=>'经营管理','恐怖惊悚'=>'恐怖悬疑','企业管理'=>'经营管理','动漫剧本'=>'动漫同人','诗歌散文'=>'散文诗词','武侠江湖'=>'快意江湖',
        '专职教育'=>'学术科研','人物传记'=>'名人传记','传记'=>'名人传记','小说同人'=>'动漫同人','影视同人'=>'动漫同人','恩怨情仇'=>'都市生活','职场白领'=>'白领生涯',
        '法律法规'=>'社会科学','烹饪美食'=>'生活百科','网络安全'=>'计算机类','金融投资'=>'经营管理','其它小说'=>'其他书籍','国术武技'=>'学术科研',
        '宦海风云'=>'官场都市','育儿知识'=>'生活百科','计算机基础'=>'计算机类','销售技巧'=>'经营管理','商海沉浮'=>'都市生活','玄幻仙侠'=>'玄幻小说',
        '古典文学'=>'古典名著'  ,'世界名著'=>'中外名著','TXT版'=>'玄幻小说','学术研究'=>'学术科研','古典奇幻'=>'异世大陆','美容服饰'=>'生活百科',
        '外国名著'=>'中外名著','爱在职场'=>'白领生涯','儿童文学'=>'童话寓言','外国历史'=>'中外名著','网游剧本'=>'虚拟网游','古龙武侠小说'=>'武侠小说',
        '诺贝尔文学奖获奖作品'=>'现代文学','中学教育'=>'学术科研','高等教育'=>'学术科研','旅游出行'=>'社会交际',
        '流行时尚'=>'社会交际','异界大陆'=>'异世大陆','更多作家一'=>'纪实文学','科幻小说'=>'科幻世界','玄幻奇幻'=>'科幻玄幻',
        '都市小说'=>'都市生活','网游小说'=>'虚拟网游','恐怖小说'=>'恐怖悬疑','女生言情'=>'浪漫言情','文学名著'=>'中外名著',
        '经管'=>'经营管理','法律教育'=>'社会科学','言情'=>'浪漫言情','都市'=>'都市异能','穿越'=>'穿越架空','耽美'=>'耽美纯爱',
        '科幻悬疑'=>'恐怖悬疑','同人'=>'动漫同人','科幻灵异'=>'科幻玄幻','同人小说'=>'动漫同人',
    );

    foreach($arr as $k=>$v){
        $k = trim($k);
        if($k == $betype) {
            $betype = $v;
            break;
        }
    }

    return $betype;
}
